import 'package:flutter/material.dart';
import 'package:flutter_project/common/route_page/route_name.dart';
import 'package:flutter_project/home/app_main_page.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../config/common_string_config.dart';
import '../pay/wechat_pay_utils.dart';
import '../privacy/agree_privacy_page.dart';
import '../privacy/dialog_common_view.dart';
import 'guide_swiper_page.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const SizedBox(height: 180),
              Center(
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 40),
                  child: Image.asset('assets/images/ic_launcher.png'),
                ),
              ),
              const Spacer(),
              const Spacer(),
            ],
          ),
          Positioned(
            bottom: 40,
            left: 0,
            right: 0,
            child: Center(
              child: Text(
                CommonStringConfig.appTitle,
                style:
                    const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    init();
    WechatPayUtils().initFlutterWX();
  }

  init() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      shareSave().then((value){
        if(value==true){
          Future.delayed(const Duration(milliseconds:500),(){
            RouteNames.pushReplacement(AppMainPage());
          });
        }else{
          ConfirmAlertDialog.showPrivacyDialog(context,
              childWidget: const AgreePrivacyPage());
        }
      });

    });
  }

  Future<bool> shareSave() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    var hasAgree = prefs.getBool('hasAgree');
    if (hasAgree != null) {
      return hasAgree;
    }
    return false;
  }
}
